<?php
class Html
{
    public static function paginate($page, $perPage, $totalItems, $action, array $params = array(), array $data = array(), $id = 'id', $title = 'title')
    {
        $adjacence = 2;
        $page = $page != 0 ? $page : 1;
        $totalPages = ceil($totalItems / $perPage);

        Loader::getInstance()->helper('Link');

        $link = Link::build($action, $data, $id, $title);

        $paramText = '?';
        foreach ($params as $key => $param)
        {
            $paramText .= $key . '=' . $param . '&';
        }

        $link .= $paramText . 'page=';

        $tpl = '<ul class="paging clearfix">';

        $start = ($start = $page - $adjacence) > 2? $start : 1;
        $end = ($end = $page + $adjacence) < ($totalPages - 1)? $end : $totalPages;

        if ($start > 1)
        {
            $tpl .= '
                <li><a href="' . $link . 1 . '">1</a></li>
                <li>...</li>
            ';
        }

        for ($i = $start; $i <= $end; $i++)
        {
            if ($i == $page)
            {
                $tpl .= '<li><a href="' . $link . $i . '" class="paging-active">' . $i . '</a></li>';
            }
            else
            {
                $tpl .= '<li><a href="' . $link . $i . '">' . $i . '</a></li>';
            }
        }


        if ($end < $totalPages)
        {
            $tpl .= '
                <li>...</li>
                <li><a href="' . $link . $totalPages . '">' . $totalPages . '</a></li>
            ';
        }

        $tpl .= '</ul>';

        return $tpl;
    }

    public static function date($time)
    {
        return date('Y-m-d', $time);
    }
}